package com.bsc.rtc.lib.libraries.RxJavaRetrofitDemo.ServiceInterfaces;

import android.util.Log;

import com.bsc.rtc.lib.libraries.RxJavaRetrofitDemo.entity.Translation;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitRequest {
    public void getCall(Observer<Translation> observer) {
        retrofitsService.getCall().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }

    public Observable<Translation> getCall2() {
        return retrofitsService.getCall();
    }
    public Observable<Translation> getCall3() {
        return retrofitsService.getCall();
    }

    /**
     * Retrofit实例封装
     */
    private static RetrofitRequest instance;
    private static Retrofit retrofit;
    // b. 创建 网络请求接口 的实例
    private RetrofitsService retrofitsService;

    public RetrofitRequest() {
        retrofit = new Retrofit.Builder()
                .baseUrl("http://fy.iciba.com/") // 设置 网络请求 Url
                .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 支持RxJava
                .build();
        retrofitsService = retrofit.create(RetrofitsService.class);
        Log.i("RetrofitActivity", "RetrofitRequest created");

    }

    public static RetrofitRequest getInstance() {
        if (instance == null) {
            instance = new RetrofitRequest();
        }
        return instance;
    }
}
